package ua.tach.shortl.logic;

import com.badlogic.gdx.graphics.GL10;

public class Drezina extends Train {
	public static final float SPEED = 0.2f;
	public static final float REPAIR_RATE = 0.015f;

	private Wagon drezina;

	public Drezina(int c, RailPosition nr, float s) {
		RailPosition r = new RailPosition(nr);
		owner = Country.CITY_COLORS[6];
		movingDirection = true;
		this.speed  = SPEED;
		maxSpeed = SPEED;
		r.train = this;
		trainSize = 0;
		waiting = true;
		drezina = Train.getWagon(7, r, this);
		tick();
	}

	public void dispose() {
		drezina.isFree = true;
	}

	public boolean tick () {
		reqToTurn = false;
		boolean repair = false;
		drezina.tick();
		if (waiting) return false;

		if(reqToTurn)
			movingDirection = !movingDirection ;

		// Repair rails
		head.set( drezina.current.renderCoord.x, drezina.current.renderCoord.y);
		for (CrashPlace c : Country.crashes) {
			if (c.dist(head) < RENDERSIZE) {
				repair = true;
				if(speed>0) speed -= REPAIR_RATE;
				else {
					Country.crashes.remove(c);
					Country.bank.getMoney(1);
				}
			}
		}
				
		if (!repair)
			drezina.checkWay();

		if(drezina.current.atDepot)
			return true;

		if (wagonCrash(drezina.current.base)) {
			drezina.isFree = true;
			return true;
		}

		return false;
	}

	public void render (GL10 gl) {
		gl.glEnable(GL10.GL_BLEND);
		gl.glColor4f(1.f, 1.f, 1.f, 1.f);
		drezina.render(gl);
	}
}
